<html>
<head><meta charset="utf-8"><title>void as usize · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html">void as usize</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="201712715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201712715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201712715">(Jun 23 2020 at 11:17)</a>:</h4>
<p>Is this expected behavior?</p>
<div class="codehilite"><pre><span></span><code><span class="k">enum</span> <span class="nc">One</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">one_as_usize</span><span class="p">(</span><span class="n">x</span>: <span class="nc">One</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// works</span>
<span class="k">enum</span> <span class="nc">Two</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">two_as_usize</span><span class="p">(</span><span class="n">x</span>: <span class="nc">Two</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// works</span>
<span class="k">enum</span> <span class="nc">Zero</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">zero_as_usize</span><span class="p">(</span><span class="n">x</span>: <span class="nc">Zero</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// fails, non-primitive cast</span>
</code></pre></div>



<a name="201712812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201712812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201712812">(Jun 23 2020 at 11:18)</a>:</h4>
<p>this edge case broke some macro code of mine</p>



<a name="201712846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201712846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201712846">(Jun 23 2020 at 11:19)</a>:</h4>
<p>is the problem that empty enums don't count as "C-style"?</p>



<a name="201712940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201712940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201712940">(Jun 23 2020 at 11:20)</a>:</h4>
<p>Empty enum in C are not zero sized IIRC, where they are in Rust</p>



<a name="201712963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201712963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201712963">(Jun 23 2020 at 11:20)</a>:</h4>
<p>I get that, but it seems a bit weird for this aspect of the semantics to be affecting <code>as usize</code> casts</p>



<a name="201713066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201713066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201713066">(Jun 23 2020 at 11:22)</a>:</h4>
<p>With repr(C) does it work ? (Pure supposition, untested)</p>



<a name="201713108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201713108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201713108">(Jun 23 2020 at 11:23)</a>:</h4>
<p>the <code>repr(C)</code> itself fails: <code>unsupported representation for zero-variant enum</code></p>



<a name="201713192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201713192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201713192">(Jun 23 2020 at 11:24)</a>:</h4>
<p>That’s logical at least ^^ then I don’t think there is an automatic way to do it</p>



<a name="201713218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201713218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201713218">(Jun 23 2020 at 11:24)</a>:</h4>
<p>You’ll probably have to return 0 directly or something like that</p>



<a name="201713351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201713351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201713351">(Jun 23 2020 at 11:26)</a>:</h4>
<p>I think so</p>



<a name="201754171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201754171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201754171">(Jun 23 2020 at 17:09)</a>:</h4>
<p><code>x</code> is uninhabited, it is UB to ever execute that code</p>



<a name="201754187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201754187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201754187">(Jun 23 2020 at 17:09)</a>:</h4>
<p>so it would actually make sense to allow a cast, as it is dead code anyway^^</p>



<a name="201754206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201754206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201754206">(Jun 23 2020 at 17:09)</a>:</h4>
<p>I guess nobody ever considered it worthwhile, but macros are a good argument</p>



<a name="201754277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201754277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201754277">(Jun 23 2020 at 17:09)</a>:</h4>
<p>so I'd say that yes, currently it is expected behavior, but it seems like a reasonable thing to make this more consistent</p>



<a name="201754329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201754329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201754329">(Jun 23 2020 at 17:10)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> feel free to open an issue for this; Cc <code>@RalfJung</code> so I can tag some people in it</p>



<a name="201758948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201758948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201758948">(Jun 23 2020 at 17:50)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Do you know if the implementation should be <code>0</code> vs <code>match x {}</code>? The discussions about <code>&amp;!</code> have left me at a loss about how uninhabited void really is</p>



<a name="201759213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759213">(Jun 23 2020 at 17:52)</a>:</h4>
<p>Afaict you are casting <code>Zero</code>, not <code>&amp;Zero</code> (which would cast the pointer). So you should be able to just <code>match x {}</code></p>



<a name="201759303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759303">(Jun 23 2020 at 17:53)</a>:</h4>
<p>sure, I mean both expressions are well typed</p>



<a name="201759369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759369">(Jun 23 2020 at 17:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/void.20as.20usize/near/201758948">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> Do you know if the implementation should be <code>0</code> vs <code>match x {}</code>? The discussions about <code>&amp;!</code> have left me at a loss about how uninhabited void really is</p>
</blockquote>
<p>lol^^ it's as uninhabited as it gets ;)</p>



<a name="201759384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759384">(Jun 23 2020 at 17:54)</a>:</h4>
<p>but it's possible that they have a different semantic effect, since one "examines" x and the other doesn't</p>



<a name="201759417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759417">(Jun 23 2020 at 17:54)</a>:</h4>
<p>the implementation should be <code>Unreachable</code>, IMO</p>



<a name="201759437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759437">(Jun 23 2020 at 17:54)</a>:</h4>
<p>which I guess is the same as <code>match x {}</code></p>



<a name="201759458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759458">(Jun 23 2020 at 17:54)</a>:</h4>
<p><code>0</code> seems... rather arbitrary^^</p>



<a name="201759487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759487">(Jun 23 2020 at 17:55)</a>:</h4>
<p>it's also actual code</p>



<a name="201759527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759527">(Jun 23 2020 at 17:55)</a>:</h4>
<p>I think that if you codegen them with <code>match x {}</code> you won't even get a return</p>



<a name="201759723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759723">(Jun 23 2020 at 17:57)</a>:</h4>
<p>It seems like you might even be able to delete the entire function in codegen</p>



<a name="201759774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201759774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201759774">(Jun 23 2020 at 17:57)</a>:</h4>
<p>I'm not really sure how aggressively <code>Unreachable</code> gets optimized</p>



<a name="201761939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201761939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201761939">(Jun 23 2020 at 18:14)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/void.20as.20usize/near/201759723">said</a>:</p>
<blockquote>
<p>It seems like you might even be able to delete the entire function in codegen</p>
</blockquote>
<p>yeah that is certainly the plan</p>



<a name="201761996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/void%20as%20usize/near/201761996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/void.20as.20usize.html#201761996">(Jun 23 2020 at 18:15)</a>:</h4>
<p>in fact even functions taking <code>&amp;!</code> we want to entirely delete. that is one of the things that makes the discussion in <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/77">https://github.com/rust-lang/unsafe-code-guidelines/issues/77</a> so complicated.^^</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>